//
// Created by root on 11/25/21.
//
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

void get_simd_type() {
  std::ifstream file("/proc/cpuinfo", std::ios::in);
  std::string line;
  std::vector<std::string> flags = {"SSE", "AVX", "FMA", "AVX512F"};
  std::string cpu_simd = "";
  while (getline(file, line)) {
    std::string::size_type index = line.find("flags");
    if (index != std::string::npos) {
      for (int i = 0; i < flags.size(); ++i) {
        auto flag = flags[i];
        std::transform(flag.begin(), flag.end(), flag.begin(), tolower);
        index = line.find(flag);
        if (index != std::string::npos) {
          if (flag == "AVX") {
#define __X86_AVX__
          }
          if (flag == "SSE") {
#define __X86_SSE__
          }
          if (flag == "AVX512F") {
#define __X86_AVX512F__
          }
          if (flag == "FMA") {
#define __X86_FMA__
          }
          cpu_simd += flags[i] + " ";
        }
      }
      break;
    }
  }
  std::cout << "This Cpu support SIMD instructions is [" << cpu_simd << "]" << std::endl;
}

int main() {
  get_simd_type();
}
